package com.kordar.admin.exception;

import com.kordar.auth.AuthException;
import com.kordar.datalist.ResultEntity;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

@ControllerAdvice
@RestController
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BindException.class)
    public ResultEntity<Object> validationExceptionHandler(BindException e) {
        ResultEntity<Object> ret = new ResultEntity<>();
        ret.setCode(2);
        if (!CollectionUtils.isEmpty(e.getBindingResult().getAllErrors())) {
            ret.setMsg(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        } else {
            ret.setMsg(e.getMessage());
        }
        return ret;
    }

    /*// 方法参数校验异常
    @ExceptionHandler(value = ConstraintViolationException.class)
    public ResultEntity<Object> constraintViolationException(Exception e) {
        if (e.getMessage() != null) {
            int index = e.getMessage().indexOf(":");
            return new ResultEntity<>(2, index != -1 ?
                    e.getMessage().substring(index + 1).trim() : e.getMessage());
        }
        return new ResultEntity<>(2, "");
    }


    // Bean 校验异常
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultEntity<Object> notValidExceptionHandler(MethodArgumentNotValidException e) {
        System.out.println("=================================");
        System.out.println(e);
        ResultEntity<Object> ret = new ResultEntity<>();
        ret.setCode(2);
        if (!CollectionUtils.isEmpty(e.getBindingResult().getAllErrors())) {
            ret.setMsg(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        } else {
            ret.setMsg(e.getMessage());
        }
        return ret;
    }*/

    @ExceptionHandler(value = AuthException.class)
    public ResultEntity<Object> authExceptionHandler(AuthException e) {
        return new ResultEntity<>(2, e.getMessage());
    }

    /*@ExceptionHandler(ShiroException.class)
    public ResultEntity<Object> doHandleShiroException(ShiroException e) {
        if (e instanceof UnknownAccountException) {
            return ResultEntity.error("账户不存在");
        } else if (e instanceof LockedAccountException) {
            return ResultEntity.error("账户已被禁用");
        } else if (e instanceof IncorrectCredentialsException) {
            return ResultEntity.error("密码不正确");
        } else if (e instanceof AuthorizationException) {
            return ResultEntity.error("没有此操作权限");
        } else {
            return ResultEntity.error("系统维护中");
        }
    }*/
}
